package com.jprocms.module.cms.controller.admin.investigate;

import com.jpro.framework.common.pojo.CommonResult;
import com.jpro.framework.common.pojo.PageResult;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.jprocms.module.cms.controller.admin.investigate.vo.InvestigateCreateReqVO;
import com.jprocms.module.cms.controller.admin.investigate.vo.InvestigatePageReqVO;
import com.jprocms.module.cms.controller.admin.investigate.vo.InvestigateRespVO;
import com.jprocms.module.cms.controller.admin.investigate.vo.InvestigateUpdateReqVO;
import com.jprocms.module.cms.convert.investigate.InvestigateConvert;
import com.jprocms.module.cms.dal.dataobject.investigate.InvestigateDO;
import com.jprocms.module.cms.service.investigate.InvestigateService;
import com.jprocms.module.cms.util.CmsContextUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import static com.jpro.framework.common.pojo.CommonResult.success;
import static com.jprocms.module.cms.web.Directives.getOrderItems;

@Tag(name = "管理后台 - 问卷调查")
@RestController
@RequestMapping("/cms/investigate")
@Validated
public class InvestigateController {

    @Resource
    private InvestigateService investigateService;

    @PostMapping("/create")
    @Operation(summary = "创建问卷调查")
    @PreAuthorize("@ss.hasPermission('cms:investigate:create')")
    public CommonResult<Long> createInvestigate(@Valid @RequestBody InvestigateCreateReqVO createReqVO) {
        createReqVO.setSiteId(CmsContextUtil.getCurrSiteId());
        createReqVO.init();
        return success(investigateService.createInvestigate(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新问卷调查")
    @PreAuthorize("@ss.hasPermission('cms:investigate:update')")
    public CommonResult<Boolean> updateInvestigate(@Valid @RequestBody InvestigateUpdateReqVO updateReqVO) {
        investigateService.updateInvestigate(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除问卷调查")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('cms:investigate:delete')")
    public CommonResult<Boolean> deleteInvestigate(@RequestParam("id") Long id) {
        investigateService.deleteInvestigate(id);
        return success(true);
    }

    @DeleteMapping("/deleteByIds")
    @Operation(summary = "批量删问卷调查")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('cms:investigate:delete')")
    public CommonResult<Boolean> deleteByIds(@RequestBody List<Long> ids) {
        investigateService.deleteByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得问卷调查")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('cms:investigate:query')")
    public CommonResult<InvestigateRespVO> getInvestigate(@RequestParam("id") Long id) {
        InvestigateDO investigate = investigateService.getInvestigate(id);
        final InvestigateRespVO investigateRespVO = InvestigateConvert.INSTANCE.convert(investigate);
        return success(investigateRespVO);
    }

    @GetMapping("/list")
    @Operation(summary = "获得问卷调查列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('cms:investigate:query')")
    public CommonResult<List<InvestigateRespVO>> getInvestigateList(@RequestParam("ids") Collection<Long> ids) {
        List<InvestigateDO> list = investigateService.getInvestigateList(ids);
        return success(InvestigateConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得问卷调查分页")
    @PreAuthorize("@ss.hasPermission('cms:investigate:query')")
    public CommonResult<PageResult<InvestigateRespVO>> getInvestigatePage(@Valid InvestigatePageReqVO pageVO) {
        pageVO.setSiteId(CmsContextUtil.getCurrSiteId());
        //排序值降序，ID降序排序
        Collection<OrderItem> orderItems = getOrderItems(Arrays.asList("sort_num_asc", "id_asc"));
        pageVO.setOrderItems(orderItems);
        PageResult<InvestigateDO> pageResult = investigateService.getInvestigatePage(pageVO);
        return success(InvestigateConvert.INSTANCE.convertPage(pageResult));
    }
}
